/**
 * 
 */
package com.suyx.job.service.area.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;

import com.suyx.job.entity.RoleInfo;
import com.suyx.job.entity.TuInfo;
import com.suyx.job.service.ITuInfoService;
import com.suyx.job.service.area.IGoAreaService;
import com.suyx.job.utils.ImagePHash;
import com.suyx.job.utils.ImageUtils;
import com.suyx.job.utils.ScreenCapture;
import com.suyx.job.utils.SysStaticData;

import lombok.extern.slf4j.Slf4j;
/**
 * @author syx
 *
 */
@Slf4j
public abstract class AbstractAreaService implements IGoAreaService{

	@Autowired
    private ITuInfoService tuInfoService;
	
	protected void userCA1(String device,String[] infos){
		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
		ScreenCapture.mouseClick(device, "730 224");// 打开ca 1
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0]+" "+infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}
	
	protected void userCA2(String device,String[] infos){
		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
		ScreenCapture.mouseClick(device, "716 308");// 打开ca 2
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0]+" "+infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}
	
	protected void userCS1(String device,String[] infos){
		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
		ScreenCapture.mouseClick(device, "798 224");// 打开cs 1
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0]+" "+infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}
	
	protected void userAL1(String device,String[] infos){
		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
		ScreenCapture.mouseClick(device, "890 224");// 打开al 1
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0]+" "+infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}
	
	protected void userZZ1(String device,String[] infos){
		ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
		ScreenCapture.mouseClick(device, "981 224");// 打开zz 1
		ScreenCapture.mouseClick(device, "522 422");// 使用
		ScreenCapture.mouseClick(device, infos[0]+" "+infos[1]);
		ScreenCapture.mouseClick(device, "1156 52");// 关闭包裹
	}
	
	public void startGoto(RoleInfo roleInfo, String path, TuInfo tuInfo) throws Exception {
		String device = roleInfo.getDevice();
		while (true) {
			ScreenCapture.snapshot(device, path, "screen.png");

			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_.png"),
					120, 48, 63, 36);

			Thread.sleep(2 * 1000L);
			ScreenCapture.snapshot(device, path, "screen.png");
			ImageUtils.cutPNG(new FileInputStream(path + "screen.png"), new FileOutputStream(path + device + "_1.png"),
					120, 48, 63, 36);
			ImagePHash p = new ImagePHash();
			String image1 = p.getHash(new FileInputStream(new File(path + device + "_.png")));
			String image2 = p.getHash(new FileInputStream(new File(path + device + "_1.png")));
			int score0 = p.distance(image1, image2);
			log.info("识别" + roleInfo.getRoleName() + "是否到任务坐标点：" + score0);
			
			if (score0 == 0) {
				log.info(roleInfo.getRoleName() + "挖图任务: 到达坐标点"+ tuInfo.getScene()+ 
	                     "("+tuInfo.getZuobiaoX()+" "+tuInfo.getZuobiaoY()+")");
				// 使用藏宝图
				ScreenCapture.mouseClick(device, "1308 675");//  # 打开包裹
				Thread.sleep(5 * 100L);
        		ScreenCapture.mouseClick(device, tuInfo.getBgLocation().replace("&", " ")) ;//点击藏宝图包裹
        		ScreenCapture.mouseClick(device, "522 422");//#使用
        		ScreenCapture.mouseClick(device,"1156 52");//#关闭包裹
        		ScreenCapture.snapshot(device,path, "screen.png");
               
                ImageUtils.cutPNG(new FileInputStream(path +"screen.png"),
                        new FileOutputStream(path + device + ".png"), 1339,133,88,84);

                image1 = p.getHash(new FileInputStream(new File(path + device + ".png")));
                image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH +"zd1.png")));
                int score =  p.distance(image1, image2);
                if(score < 10){
                	//使用法术
    				ScreenCapture.mouseClick(roleInfo.getDevice(), "1288 431");
    				//使用随机目标
    				ScreenCapture.mouseClick(roleInfo.getDevice(), "1385 573");
    				ScreenCapture.mouseClick(roleInfo.getDevice(), "1390 681"); 
    				//判断战斗结束与否
    				while (true) {
    					Thread.sleep(2 * 1000L);
    					ScreenCapture.snapshot(device, path, "screen.png");
    					ImageUtils.cutPNG(new FileInputStream(path + "screen.png"),
    							new FileOutputStream(path + device + "_zd.png"), 901, 660, 30, 30);
    					image1 = p.getHash(new FileInputStream(new File(path + device + "_zd.png")));
    					image2 = p.getHash(new FileInputStream(new File(SysStaticData.SC_PIC_PATH + "zd_over.png")));
    					int score_over = p.distance(image1, image2);
    					if (score_over < 10) {
    						//ScreenCapture.mouseClick(device, "670 589");
    						// 取消自动自动
    						ScreenCapture.mouseClick(device, "1242 558");// #
    						break;
    					}
    				}	

    				// # 加血
    				ScreenCapture.mouseClick(device, "1400 40");//
    				ScreenCapture.mouseClick(device, "1193 109");// # 加血
    				ScreenCapture.mouseClick(device, "1400 40");//
    				ScreenCapture.mouseClick(device, "1359 109");// # 使用
    				ScreenCapture.mouseClick(device, "1258 33");//
    				ScreenCapture.mouseClick(device, "992 103");// # 使用
                }
			
				break;
			}
		}
	}
}
